package com.music.action.authority;

import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.music.action.BaseAction;
import com.music.action.BaseKey;
import com.music.authority.service.LoginService;
import com.music.common.ParamUtil;
import com.music.domain.User;

/**
 * 登录相关的的action请求
 * @author LFSenior
 *
 */
public class LoginAction extends BaseAction {
	/**
	 * 登录服务
	 */
	@Autowired
	LoginService loginService;
	/**
	 * 登录请求
	 * @return
	 */
	@Action(value="signing",results={@Result(name=POST,type="json",params={"root","reParam"})})
	public String signing(){
		try {
			Map<String, Object> param = ParamUtil.buildRequest(ServletActionContext.getRequest());
			User loginUser = loginService.login(param.get("email").toString(), param.get("pwd").toString());
			if(loginUser!=null){
				//登录成功
				session.put(BaseKey.LOGIN_USER, loginUser);
			}else{
				//登录失败
				throw new  RuntimeException();
			}
			reParam.put("result", "T");
		} catch (Exception e) {
			reParam.put("result", "F");
		}
		return POST;
	}
	
	/**
	 * 跳转到主页面
	 * @return
	 */
	@Action(value="index",results={@Result(name="index",location="index.jsp")})
	public String redicterIndex(){
		return "index";
	}
	
	/**
	 * 加载登录成功的页面
	 * @return
	 */
	@Action(value="loginsuccess",results={@Result(name="loginsuccess",location="core/loginsuccess.jsp")})
	public String redicterLoginSuccess(){
		return "loginsuccess";
	}
	
	/**
	 * 加载为登录的页面
	 * @return
	 */
	@Action(value="notlogin",results={@Result(name="notlogin",location="core/notlogin.jsp")})
	public String redicterNotLogin(){
		return "notlogin";
	}
}
